﻿using UnityEngine;
using System.Collections;

class GamePlaying : MonoBehaviour {

    public GameObject canvas;
    private int characterNum = 1;
    private int movements = 0;
    private GameObject player;
    private CharacterHandler charHandler;

    void Start () {
        Screen.orientation = ScreenOrientation.LandscapeLeft;
        player = GameObject.FindGameObjectWithTag("character" + characterNum);
        charHandler = (CharacterHandler) player.GetComponent<CharacterHandler>();
        movements = charHandler.getSpeed();
    }

    void Update () {
        

        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit)) {

                if (hit.collider.gameObject.name != "Player") {
                    if (Mathf.Abs((hit.collider.gameObject.transform.position - player.transform.position).sqrMagnitude) < 0.2) {
                        player.transform.position = hit.collider.gameObject.transform.position;

                        movements--;
                        if (movements == 0) {
                            if (characterNum != 5) characterNum++;
                            else characterNum = 1;

                            player = GameObject.FindGameObjectWithTag("character" + characterNum);
                            charHandler = (CharacterHandler) player.GetComponent<CharacterHandler>();
                            movements = charHandler.getSpeed();
                        }
                    }
                }
            }
        }
    }
}
